﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Data.Common;

namespace DALL.SysManage
{
  public  class T_SYS_LOG
    {
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            string strsql = "select max(LOGID)+1 from T_SYS_LOG";
            Database db = DatabaseFactory.CreateDatabase();
            object obj = db.ExecuteScalar(CommandType.Text, strsql);
            if (obj != null && obj != DBNull.Value)
            {
                return int.Parse(obj.ToString());
            }
            return 1;
        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(decimal LOGID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_SYS_LOG");
            strSql.Append(" where LOGID=" + LOGID + " ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Model.SysManage.T_SYS_LOG model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_SYS_LOG(");
            strSql.Append("LOGID,OPERATIONTIME,USERID,USERNAME,ORGCODE,USERDEPT,USERSYMBOL,OPERATIONRESULT,MODULENAME,OPERATIONOBJECT,ERRORCONTENT");
            strSql.Append(")");
            strSql.Append(" values (");
            if (model.LOGID == null)
            {
                strSql.Append("null,");
            }
            else
            {
                strSql.Append("" + model.LOGID + ",");
            }
            strSql.Append("'" + model.OPERATIONTIME + "',");
            if (model.USERID == null)
            {
                strSql.Append("null,");
            }
            else
            {
                strSql.Append("" + model.USERID + ",");
            }
            strSql.Append("'" + model.USERNAME + "',");
            strSql.Append("'" + model.ORGCODE + "',");
            strSql.Append("'" + model.USERDEPT + "',");
            strSql.Append("'" + model.USERSYMBOL + "',");
            strSql.Append("'" + model.OPERATIONRESULT + "',");
            strSql.Append("'" + model.MODULENAME + "',");
            strSql.Append("'" + model.OPERATIONOBJECT + "',");
            strSql.Append("'" + model.ERRORCONTENT + "'");
            strSql.Append(")");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Model.SysManage.T_SYS_LOG model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_SYS_LOG set ");
            strSql.Append("OPERATIONTIME='" + model.OPERATIONTIME + "',");
            if (model.USERID == null)
            {
                strSql.Append("USERID=null,");
            }
            else
            {
                strSql.Append("USERID=" + model.USERID + ",");
            }
            strSql.Append("USERNAME='" + model.USERNAME + "',");
            strSql.Append("ORGCODE='" + model.ORGCODE + "',");
            strSql.Append("USERDEPT='" + model.USERDEPT + "',");
            strSql.Append("USERSYMBOL='" + model.USERSYMBOL + "',");
            strSql.Append("OPERATIONRESULT='" + model.OPERATIONRESULT + "',");
            strSql.Append("MODULENAME='" + model.MODULENAME + "',");
            strSql.Append("OPERATIONOBJECT='" + model.OPERATIONOBJECT + "',");
            strSql.Append("ERRORCONTENT='" + model.ERRORCONTENT + "'");
            strSql.Append(" where LOGID=" + model.LOGID + " ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(decimal LOGID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_SYS_LOG ");
            strSql.Append(" where LOGID=" + LOGID + " ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysManage.T_SYS_LOG GetModel(decimal LOGID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select   ");
            strSql.Append(" LOGID,OPERATIONTIME,USERID,USERNAME,ORGCODE,USERDEPT,USERSYMBOL,OPERATIONRESULT,MODULENAME,OPERATIONOBJECT,ERRORCONTENT ");
            strSql.Append(" from T_SYS_LOG ");
            strSql.Append(" where LOGID=" + LOGID + " ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            Model.SysManage.T_SYS_LOG model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LOGID,OPERATIONTIME,USERID,USERNAME,ORGCODE,USERDEPT,USERSYMBOL,OPERATIONRESULT,MODULENAME,OPERATIONOBJECT,ERRORCONTENT ");
            strSql.Append(" FROM T_SYS_LOG ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "T_SYS_LOG");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "System.Collections.Generic.List`1[Maticsoft.CodeHelper.ColumnInfo]");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.SysManage.T_SYS_LOG> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LOGID,OPERATIONTIME,USERID,USERNAME,ORGCODE,USERDEPT,USERSYMBOL,OPERATIONRESULT,MODULENAME,OPERATIONOBJECT,ERRORCONTENT ");
            strSql.Append(" FROM T_SYS_LOG ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.SysManage.T_SYS_LOG> list = new List<Model.SysManage.T_SYS_LOG>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.SysManage.T_SYS_LOG ReaderBind(IDataReader dataReader)
        {
            Model.SysManage.T_SYS_LOG model = new Model.SysManage.T_SYS_LOG();
            object ojb;
            ojb = dataReader["LOGID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.LOGID = Convert.ToDecimal(ojb);
            }
            ojb = dataReader["OPERATIONTIME"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OPERATIONTIME = Convert.ToDateTime(ojb);
            }
            ojb = dataReader["USERID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.USERID = Convert.ToDecimal(ojb);
            }
            model.USERNAME = dataReader["USERNAME"].ToString();
            model.ORGCODE = dataReader["ORGCODE"].ToString();
            model.USERDEPT = dataReader["USERDEPT"].ToString();
            model.USERSYMBOL = dataReader["USERSYMBOL"].ToString();
            model.OPERATIONRESULT = dataReader["OPERATIONRESULT"].ToString();
            model.MODULENAME = dataReader["MODULENAME"].ToString();
            model.OPERATIONOBJECT = dataReader["OPERATIONOBJECT"].ToString();
            model.ERRORCONTENT = dataReader["ERRORCONTENT"].ToString();
            return model;
        }

        #endregion  Method
    }
}
